
package com.epam.android.training.day45;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class Day45Activity extends Activity {

  private static final int DIALOG_HOW_ARE_YOU = 0;

  private static final int DIALOG_LIST = 1;

  private static final int NOTIFICATION_ID = 1;

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    TextView textView = (TextView) findViewById(R.id.textView);
    registerForContextMenu(textView);
    Button askButton = (Button) findViewById(R.id.button_ask);
    askButton.setOnClickListener(new OnClickListener() {
      @Override
      public void onClick(View v) {
        showDialog(DIALOG_LIST);
      }
    });

    Button notificateButton = (Button) findViewById(R.id.button_notificate);
    notificateButton.setOnClickListener(new OnClickListener() {
      @Override
      public void onClick(View v) {
        NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        mNotificationManager.notify(NOTIFICATION_ID, createNotification());
      }
    });

    Button startActivityButton = (Button) findViewById(R.id.button_start_activity);
    startActivityButton.setOnClickListener(new OnClickListener() {
      @Override
      public void onClick(View v) {
        Intent themeActivityIntent = new Intent(getApplicationContext(), ThemeTaskActivity.class);
        startActivity(themeActivityIntent);
      }
    });

    Button showListViewButton = (Button) findViewById(R.id.button_show_list_view);
    showListViewButton.setOnClickListener(new OnClickListener() {
      @Override
      public void onClick(View v) {
        Intent listActivityIntent = new Intent(getApplicationContext(), TestListActivity.class);
        startActivity(listActivityIntent);
      }
    });

  }

  private Notification createNotification() {
    Notification notification = new Notification(R.drawable.icon, getResources().getString(
        R.string.my_notification_ticker_text), System.currentTimeMillis());
    Intent notificationIntent = new Intent(getApplicationContext(), Day45Activity.class);
    PendingIntent contentIntent = PendingIntent.getActivity(getApplicationContext(), 0, notificationIntent, 0);
    notification.setLatestEventInfo(getApplicationContext(),
        getResources().getString(R.string.my_notification_content_title),
        getResources().getString(R.string.my_notification_content_text), contentIntent);
    return notification;
  }

  @Override
  protected Dialog onCreateDialog(int id) {
    Dialog dialog = null;
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    switch (id) {
      case DIALOG_HOW_ARE_YOU:
        builder
            .setMessage(getResources().getString(R.string.dialog_how_are_you_message))
            .setCancelable(false)
            .setPositiveButton(getResources().getString(R.string.dialog_how_are_you_positive),
                new DialogInterface.OnClickListener() {
                  @Override
                  public void onClick(DialogInterface dialog, int id) {
                    dialog.cancel();
                  }
                })
            .setNegativeButton(getResources().getString(R.string.dialog_how_are_you_negative),
                new DialogInterface.OnClickListener() {
                  @Override
                  public void onClick(DialogInterface dialog, int id) {
                    Day45Activity.this.finish();
                  }
                });
        dialog = builder.create();
        break;
      case DIALOG_LIST:
        final CharSequence[] DIALOG_LIST_ITEMS = getResources().getStringArray(R.array.dialog_list_items);
        builder.setTitle(getResources().getString(R.string.dialog_list_title));
        builder.setItems(DIALOG_LIST_ITEMS, new DialogInterface.OnClickListener() {
          @Override
          public void onClick(DialogInterface dialog, int item) {
            Toast.makeText(getApplicationContext(), DIALOG_LIST_ITEMS[item], Toast.LENGTH_SHORT).show();
          }
        });
        dialog = builder.create();
        break;
      default:
        dialog = super.onCreateDialog(id);
    }
    return dialog;
  }

  @Override
  public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
      case R.id.checkable_item:
        showDialog(DIALOG_HOW_ARE_YOU);
        return true;
      case R.id.bad_help:
        return true;
      default:
        return super.onOptionsItemSelected(item);
    }
  }

  @Override
  public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
    super.onCreateContextMenu(menu, v, menuInfo);
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.context_menu, menu);
  }

  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.main_menu, menu);
    return true;
  }
}
